public class Solution {
    public ListNode mergeTwoLists(ListNode headA, ListNode headB) {
        if(headA == null && headB == null) return null;
        if(headA == null && headB != null) return headB;
        if(headA != null && headB == null) return headA;
        ListNode newHead = new ListNode(-1);
        ListNode temp = newHead;
        while(headA != null && headB != null){
            if(headA.val >= headB.val){
                temp.next = headB;
                headB = headB.next;
            }else{
                temp.next = headA;
                headA = headA.next;
            }
            temp = temp.next;
        }
        while(headA != null){
            temp.next = headA;
            headA = headA.next;
            temp = temp.next;
        }
        while(headB != null){
            temp.next = headB;
            headB = headB.next;
            temp = temp.next;
        }
        return newHead.next;
    }
}
